﻿using System; 
using System.Linq; 
using AppSocketModel;
using AppSocketService.Account;
using AppSocketService.Product;
using AppSocketService.ProductPerson;
using BCGPSRESTful.WebApiConfig;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BCGPSRESTful.Controllers.Phone.Account.Person
{
    /// <summary>
    /// 个人用户手机端
    /// </summary>
    [ApiExplorerSettings(GroupName = "PersonApp")]
    [Route("api/[controller]")]
    [ApiController]
    public class PhoneAccountPersonController : PhoneBaseController
    {
        private readonly TimeSpan expiration = TimeSpan.FromDays(7);
        private readonly IAccountPersonService accountPersonService;
        private readonly IProductPersonService productPersonService;


        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountPersonService"></param>
        /// <param name="productPersonService"></param>
        public PhoneAccountPersonController(IAccountPersonService accountPersonService, IProductPersonService productPersonService)
        {
            this.accountPersonService = accountPersonService;
            this.productPersonService = productPersonService;

        }
        #endregion


        #region 注册个人账户
        /// <summary>
        /// 注册个人账户
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(AccountDto), 200)]
        [AllowAnonymous]
        public ActionResult<AccountDto> Register(AccountPersonRegisterDto dto)
        {
            productPersonService.ProductCheck(dto.IMEI);
            return accountPersonService.RegisterPersonAccount(dto);
        }
        #endregion

        #region 账户登录
        /// <summary>
        /// 账户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("[action]")]
        [AllowAnonymous]
        [ProducesResponseType(typeof(TokenDto), 200)]
        public ActionResult<TokenDto> Login([FromBody] AccountLoginDto dto)
        {
            var ip = Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrWhiteSpace(ip))
                ip = HttpContext.Connection.RemoteIpAddress.ToString();

            var accountDto = accountPersonService.Login(dto, ip);

            if (accountDto == null)
                throw new ArgumentException("用户名或密码不正确!");

            accountDto.Password = "";
            return new TokenDto
            {
                Description = "WELCAME:" + accountDto.AccountId,
                AccessToken = accountDto.GenerateToken(expiration, accountDto.AccountRole),
                ExpiresIn = (int)expiration.TotalSeconds,
                EndTime = DateTime.Now.AddSeconds(expiration.TotalSeconds - 12 * 3600),
                Account = accountDto,
            };
        }

        #endregion

        #region 通过旧密码修改密码
        /// <summary>
        /// 通过旧密码修改密码
        /// </summary> 
        /// <param name="dto">密码</param>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(void), 200)]
        public ActionResult ModifyPassword([FromBody] ModifyPasswordRequestDto dto)
        {
            accountPersonService.ModifyPassword(dto);
            return this.Ok();
        }
        #endregion

        #region 通过手机验证码修改密码
        /// <summary>
        /// 通过手机验证码修改密码
        /// </summary> 
        /// <param name="dto">密码</param>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(void), 200)]
        [AllowAnonymous]
        public ActionResult SetNewPassword([FromBody] SetPasswordRequestDto dto)
        {
            accountPersonService.SetNewPasswrod(dto);
            return this.Ok();
        }
        #endregion

        #region 系统账户强制设置密码
        /// <summary>
        /// 系统账户强制设置密码
        /// </summary> 
        /// <param name="phone">手机账户</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(void), 200)]
        [ApiExplorerSettings(GroupName = "SystemAccount")]
        [AllowAnonymous]
        public ActionResult SystemAdminSetNewPassword(string phone,string password)
        {
            accountPersonService.SystemAdminSetNewPassword(phone,password);
            return this.Ok();
        }

        #endregion
    }
}
 